//
//  NSString+ACExt.m
//  ACFramework
//
//  Created by caoyq on 2018/5/23.
//  Updated by caoyq on 2018/6/22.
//

#import "NSString+ACExt.h"

@implementation NSString (ACExt)

#pragma mark - 判空

+ (BOOL)ac_isNullString: (NSString *)string {
    
    if (!string) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([string isEqualToString:@"<null>"]) {
        return YES;
    }
    if ([string isEqualToString:@"null"]) {
        return YES;
    }
    if([string isEqualToString:@"NULL"]){
        return YES;
    }
    if([string isEqualToString:@"(null)"]){
        return YES;
    }
    if (!string.length) {
        return YES;
    }
    return NO;
}

+ (BOOL)ac_isEmptyString:(NSString*)string {
    if  ([self ac_isNullString:string] || !string.length) return YES;
    return NO;
}

#pragma mark - 位置

- (NSArray *)rangesOfSubString: (NSString *)string
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:string
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:nil];
    NSArray *result = [regex matchesInString:self options:0 range:NSMakeRange(0, [self length])];
    NSMutableArray *temp = [NSMutableArray array];
    for (NSTextCheckingResult *match in result) {
        NSRange range = match.range;
        [temp addObject:[NSValue valueWithRange:range]];
    }
    return temp;
}

#pragma mark - 文字编码

- (NSString *)ac_UTF8Encode
{
    if (@available(iOS 9.0, *)) {
        return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    }
    return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)ac_UTF8Decode
{
    if (@available(iOS 9.0, *)) {
        return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    return [self stringByRemovingPercentEncoding];
}

#pragma mark - 文字宽高

- (CGSize)ac_textSizeWithFont: (UIFont *)font
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    CGSize size = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize labelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
    return labelSize;
}

- (CGFloat)ac_textHeightWithWidth: (CGFloat)width font: (UIFont *)font
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    CGSize size = CGSizeMake(width, MAXFLOAT);
    CGSize labelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
    return labelSize.height;
}

#pragma mark - 属性文字

- (NSAttributedString *)ac_addStrikethroughWithColor: (UIColor *)color
{
    NSAttributedString *attrStr =
    [[NSAttributedString alloc]initWithString: self
                                   attributes:
     @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
       NSStrikethroughColorAttributeName:color}];
    return attrStr;
}

- (NSMutableAttributedString *)ac_setColor: (UIColor *)color range: (NSRange)range
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self];
    [str addAttribute:NSForegroundColorAttributeName value:color range:range];
    return str;
}

- (NSMutableAttributedString *)ac_setFont: (UIFont *)font range: (NSRange)range
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self];
    [str addAttribute:NSFontAttributeName value:font range:range];
    return str;
}

- (NSMutableAttributedString *)ac_setColors: (NSArray *)colors ranges: (NSArray *)rangeArray
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self];
    NSInteger count = MIN(colors.count, rangeArray.count);
    for (NSInteger index = 0; index < count; index ++) {
        NSRange range = [rangeArray[index] rangeValue];
        UIColor *color = colors[index];
        [str addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    return str;
}

- (NSMutableAttributedString *)ac_setColors:(NSArray *)colors font:(NSArray *)fonts range:(NSArray *)ranges
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self];
    NSInteger count = MIN(MIN(colors.count, fonts.count), ranges.count);
    for (NSInteger idx = 0; idx < count; idx++) {
        UIColor *color = colors[idx];
        UIFont *font = fonts[idx];
        NSNumber *rangeNumber = ranges[idx];
        NSRange range = rangeNumber.rangeValue;
        [str addAttributes:@{NSForegroundColorAttributeName : color,
                             NSFontAttributeName : font} range:range];
    }
    return str;
}

@end
